package com.eimf.saf.common.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eimf.saf.base.util.Page;
import com.eimf.saf.common.dao.InternationDao;
import com.eimf.saf.common.service.InternationService;
import com.eimf.saf.common.util.CommonCache;
import com.eimf.saf.security.exception.AppException;

/**
 * @author eden
 * @date 2013-11-14 下午4:02:52
 * @Description: TODO(这里用一句话描述这个类的作用)
 */
@Service
public class InternationServiceImpl implements InternationService
{

    private static Log logger = LogFactory.getLog(InternationServiceImpl.class);

    @Autowired
    private InternationDao internationDao;

    @Override
    public List<Map<Object, Object>> findAllInternation()
    {
        return internationDao.findAllInternation();
    }

    /**
     * 新增国际化
     * */
    @Override
    public Long addInternation(final Map<String, Object> parmMap)
    {
        final Long id = internationDao.addInternation(parmMap);
        updateAppCache(id, parmMap);
        return id;
    }

    /**
     * 修改国际化信息资料.
     * */
    @Override
    public int updInternation(final Map<String, Object> map)
    {
        try
        {
            final int result = internationDao.updInternation(map);
            final Long key = Long.valueOf(((String) map.get("id")));
            updateAppCache(key, map);
            return result;
        } catch (final Exception e)
        {
            InternationServiceImpl.logger.error(e.getMessage(), e);
            throw AppException.createErrorException("操作失败！资源信息在同一语种中不能重复");
        }
    }

    @Override
    public List<Map<String, Object>> getInternationById(final long InternationId)
    {
        return internationDao.getInternationById(InternationId);
    }

    @Override
    public Map<String, Object> getInternationByName(final String cnName)
    {
        return internationDao.getInternationByName(cnName);
    }

    /**
     * 根据条件查询所有相关的国际化
     * */
    @Override
    public Page findInternation(final HashMap<String, Object> param, final Page pager)
    {
        // 先设置总行数以及查询参数
        pager.setTotalRows(param, internationDao.findCountInternation(param));
        // 然后查询并设置当前页数据
        pager.setList(internationDao.findInternation(param));
        return pager;
    }

    // 更新缓存
    public Long updateAppCache(final Long seq, final Map<String, Object> map)
    {
        final Map<String, String> cachemap = new HashMap<String, String>();
        for (final Object key : map.keySet())
        {
            final Object value = map.get(key);
            cachemap.put((key + "").toUpperCase(), value != null ? value + "" : null);
        }
        CommonCache.setInternationMap(seq.toString(), cachemap);
        return seq;
    }

}
